#include "../include/threadPool.h"
#include "../../command/command.h"
pthread_mutex_t mysql_mutex;
void *threadFunc(void *arg){
    threadPool_t *pthreadPool = (threadPool_t *)arg;
    int netfd;
    char phyPath[1024] = "/home/playba/gitLibrary/test_net-disk3/net-disk3/server/src/FILE/";
    char hashName[4096] = {0};
    int cmd;
    MYSQL* mysql;
    mysqlConnect(&mysql);
    while(1){
BEGIN:
        pthread_mutex_lock(&pthreadPool->mutex);
        while(pthreadPool->taskQueue.queueSize == 0){
            pthread_cond_wait(&pthreadPool->cond,&pthreadPool->mutex);
        }
        netfd = pthreadPool->taskQueue.front->data;
        printf("one worker get netfd = %d\n",netfd);
        deQueue(&pthreadPool->taskQueue);
        pthread_mutex_unlock(&pthreadPool->mutex);// 得到netfd开始处理任务
        char token[1024] = {0};
        char userName[1024] = {0};
        char buf[1024] = {0};

        printf("before recv\n");
        int uernameLen =0;
        recv(netfd,&uernameLen,sizeof(int),MSG_WAITALL);
        recv(netfd, userName,uernameLen, MSG_WAITALL);
        printf("userName:%s\n",userName);

        printf("after recv\n");
        /* recvToken(netfd, token); */
        int tokenLen = 0;
recv(netfd,&tokenLen,sizeof(int),MSG_WAITALL);
recv(netfd,token,tokenLen,MSG_WAITALL);
printf("recv token|%s|\n",token);

        printf("aftre token\n");
        if(userTokenDecode(token, SECRET, userName) != 0){
            int failedDecode = -1;
            send(netfd, &failedDecode, sizeof(int), MSG_NOSIGNAL);
            close(netfd);
        }else{
            int successDecode = 0;
            send(netfd, &successDecode, sizeof(int), MSG_NOSIGNAL);
        }
        printf("-----DEBUG1\n");
        int pathId = getUserRootPathIdByUsername(mysql,userName);
        printf("worker: pathId is %d\n",pathId);
        send(netfd,&pathId,sizeof(int),MSG_NOSIGNAL);

        char *filePath = (char *)calloc(4096,sizeof(char));
        strcpy(filePath,phyPath);
        printf("filePath |%s|\n",filePath);
        bzero(hashName, sizeof(hashName));
        recvCmd_p(netfd, buf, &cmd, &pathId);
        printf("DEBUG !\n");
        if(cmd == PUTS){
            recvCmd_p(netfd,hashName,&cmd,&pathId);
            if(cmd == -1){
                printf("对方打开文件失败!\n");
                close(netfd);
                goto BEGIN;
            }
            // 拼hashname
            strcat(filePath,hashName);
            recvFile(netfd,filePath,&cmd);
            my_puts(mysql,buf,pathId,hashName);
            free(filePath);
        }else if(cmd == GETS){
            my_gets(mysql,buf,pathId,hashName,phyPath);
            sendCmd_p(netfd,"1",GETS,&pathId);
            sendFile(netfd,hashName,hashName,GETS);
        }
        close(netfd);
    }
    return NULL;
}

int makeWorker(threadPool_t *pthreadPool){
    for(int i = 0; i < pthreadPool->workerArr.workerNum; i++){
        pthread_create(&pthreadPool->workerArr.tidArr[i],NULL,threadFunc,pthreadPool);
        printf("worker %d\n",i);
    }
    return 0;
}
